﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using evt;
using UnityEngine.UI;
using UnityEngine.Sprites;
using DG.Tweening;
public class TopNotice : MonoBehaviour {

	// Use this for initialization
	public int Type;
	private List<string> list = new List<string>();
	Vector3 ve3;
	void Start () {
		ve3 = transform.GetChild(0).GetChild(0).position;
		transform.localPosition = new Vector3 (10000, 0, 0);
		if (Type == 1) {
			gameObject.name = "TopNotice";
			ServerEventDispather.getInstance ().AddEventDispather (ServerEvent.SHOW_TOP_NOTICE,ShowTopNotice);
		} else {
			gameObject.name = "DownNotice";
			ServerEventDispather.getInstance ().AddEventDispather (ServerEvent.SHOW_DOWN_NOTICE,ShowTopNotice);
		}
	}
				
	private void ShowTopNotice(object go){
		if (list.Count <= 0) {
				list.Add (go as string);
				Play (go as string);
		} else {
				list.Add (go as string);
		}
	}

	private void Play(string str){
		transform.localPosition = Vector3.zero;
		transform.GetChild(0).GetChild(0).position = ve3;
		transform.GetChild(0).GetChild(0).GetComponent<Text> ().text = str;
		Tweener tweener = transform.GetChild(0).GetChild(0).transform.DOMove(
		new Vector3(-100f,ve3.y,ve3.z),8f);
		tweener.SetEase (Ease.Linear);
		tweener.onComplete = delegate() {
			list.RemoveAt (0);
			InvokeRepeating ("CheckPlay", 1f, 0f);		
		};
	}

	private void CheckPlay(){
		if (list.Count > 0) {
			Play (list [0]);
		} else {
			transform.localPosition = new Vector3 (10000,ve3.y, ve3.z);
		}
	}
	
	// Update is called once per frame
	void Update () {
				
	}
}
